<?php

namespace app\wx\service;
use think\Db;
use Exception;

/**
 * 用户管理操作数据库
 */
class UserService{
    const TABLE = 'user';

    /**
     * 查询条件
     * @param array   $data   参数
     * @return array 
     */
    static private function map($data){
        $map = [];
        if($data['userid']){
            $map['userid'] = $data['userid'];
        }
        if($data['name']){
            $map['name'] = $data['name'];
        }
        if($data['mobile']){
            $map['mobile'] = $data['mobile'];
        }
        if($data['gender']){
            $map['gender'] = $data['gender'];
        }
        if($data['email']){
            $map['email'] = $data['email'];
        }
        if($data['alias']){
            $map['alias'] = $data['alias'];
        }
        if($data['main_department']){
            $map['main_department'] = $data['main_department'];
        }
        if($data['start_time'] && $data['end_time']){ //交易时间
            $map['create_time'] = array(array("egt",$data['start_time']),array("elt",$data['end_time']));
        }
        return $map;
    }

    /**
     * 查询用户
     * @param array $param  查询条件
     * @param int $page  页面
     * @param int $limit 页面显示数量
     * @param string $field 查询字段
     * @return array
     */

    static public function findUser($param, $pp = 1, $num = 10,  $field = '*'){
        //查询条件
        $map = self::map($param);
        $count = Db::name(self::TABLE)->where($map)->count();
        //计算pp
        $pp = ceil($count / $num) < $pp ? ceil($count / $num) : $pp;
        //查询数据
        $list = Db::name(self::TABLE)->where($map)->order("id desc")->page($pp,$num)->field($field)->select();
        return [
            'list' => $list,
            'count' => $count,
            'pp' => $pp
        ];
    }

    /**
     * 添加用户
     * @param array 待添加数据
     * @return int 返回添加ID
     */
    static public function add($param)
    {
        $param['department']        = isset($param['department']) ? implode(',',$param['department']) : '';
        $param['order']             = isset($param['order']) ? implode(',',$param['order']) : '';
        $param['is_leader_in_dept'] = isset($param['is_leader_in_dept']) ? implode(',',$param['is_leader_in_dept']) : '';
        $param['create_time'] = date('Y-m-d H:i:s');
        $id = Db::name(self::TABLE)->insertGetId($param);
        if(!$id){
            error('数据添加失败，mysql异常!');
        }
        return $id;
    }

    /**
     * 修改用户
     * @param array 待添加数据
     * @return void
     */
    static public function edit($param)
    {
        $param['department']        = isset($param['department']) ? implode(',',$param['department']) : '';
        $param['order']             = isset($param['order']) ? implode(',',$param['order']) : '';
        $param['is_leader_in_dept'] = isset($param['is_leader_in_dept']) ? implode(',',$param['is_leader_in_dept']) : '';
        $param['update_time'] = date('Y-m-d H:i:s');
        $id = $param['id'];
        $result = Db::name(self::TABLE)->where('id',$id)->update($param);
        if(!$result){
            error('数据修改失败，mysql异常!');
        }
    }
}